﻿@Styles.Render("~/Content/css")
@section scripts{
    <link href="~/Scripts/Ztree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
    <script src="~/Scripts/Ztree/jquery.ztree.core.js"></script>
    @{
        if (Request["chkStyle"] != "radio")
        {
            <script src="~/Scripts/Ztree/jquery.ztree.excheck.js"></script>
        }
    }
    <script src="~/Scripts/Ztree/jquery.ztree.exedit.js"></script>
    <script src="~/Scripts/Ztree/jquery.ztree.exhide.js"></script>
    <script language="JavaScript">
        var zTreeObj;
        // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
        var setting = {
            async: {
                enable: true,
                type: "post",
                contentType: "application/json",
                dataType: "json",
                url: "/PubModelDialog/GetInsuranceCompanyTree",
                autoParam: ["ID"]
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "ID",
                    pIdKey: "PID",
                    rootPId: 0
                }
            },
            check: {
                enable: true,
                chkStyle: "@(Request["chkStyle"])",
                chkboxType: { "Y": "", "N": "" }
            },
            callback: {
                onAsyncSuccess: zTreeOnAsyncSuccess,
                onDblClick: zTreeOnDblClick,
                beforeClick: zTreeBeforeClick,
                beforeDblClick: zTreeBeforeClick
            },
            view: {
                filter: true, //是否启动过滤
                expandLevel: 3 //展开层级
            }

        };
        function zTreeBeforeClick(treeId, treeNode, clickFlag) {
            var canCheck = treeNode.level != 0 && !treeNode.isParent;
            if (!canCheck)
                layer.msg("当前节点不能选择！");
            return canCheck;//当是父节点 返回false 或者第一级 不让选取
        }
        $(document).ready(function () {
            zTreeObj = $.fn.zTree.init($("#tree"), setting, null);
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            //得到选中的节点
            $("#btnSave").click(function () {
                saveNode(index);
            });
        });

        ///双击事件
        function zTreeOnDblClick(event, treeId, treeNode) {
            zTreeObj = $.fn.zTree.init($("#tree"), setting, null);
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            var textid = "@(Request["textid"])";
            var hiddenId = "@(Request["hiddenId"])";
            parent.setSelectedTreeNode(treeNode, textid, hiddenId);
            parent.layer.close(index);
        };

        function saveNode(index) {
            var nodes;
            if ("@Request["chkStyle"]" == "radio") {
                nodes = $.fn.zTree.getZTreeObj("tree").getSelectedNodes();
            } else {
                nodes = $.fn.zTree.getZTreeObj("tree").getCheckedNodes();
            }
            var textid = "@(Request["textid"])";
            var hiddenId = "@(Request["hiddenId"])";
            parent.setSelectedTreeNode(nodes, textid, hiddenId);
            parent.layer.close(index);
        }

        //加载树完成后 将节点选中
        function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
            zTreeObj.expandAll(true);
            var arr = parent.document.getElementById('@(Request["hiddenId"])').value.split(',');
            for (var i = 0; i < arr.length; i++) {
                var node = zTreeObj.getNodeByParam("ID", arr[i]);
                if (node != null) {
                    zTreeObj.selectNode(node, true, false);
                    zTreeObj.expandNode(node.getParentNode(), true, true, true);
                }
            }
        }

        var hiddenNodes = []; //用于存储被隐藏的结点

        //过滤ztree显示数据
        function filter(txtObj) {
            //显示上次搜索后背隐藏的结点
            zTreeObj.showNodes(hiddenNodes);

            //查找不符合条件的叶子节点
            function filterFunc(node) {
                var keywords = txtObj.value;
                //如果子节点中有满足条件的或者本身满足的，不被隐藏返回false
                if (node.name.toLowerCase().indexOf(keywords.toLowerCase()) != -1 || hasMatchChild(node.children, keywords)) return false;
                return true;
            };

            function hasMatchChild(nodes, keywords) {
                if (!nodes || nodes.length < 1)
                    return false;
                else {
                    for (var i = 0; i < nodes.length; i++) {
                        var node = nodes[i];
                        //递归
                        if (node.name.toLowerCase().indexOf(keywords.toLowerCase()) != -1 || hasMatchChild(node.children, keywords))
                            return true;
                    }
                }
                return false;
            }

            //获取不符合条件的叶子结点
            hiddenNodes = zTreeObj.getNodesByFilter(filterFunc);

            //隐藏不符合条件的叶子结点
            zTreeObj.hideNodes(hiddenNodes);
        };


    </script>
}
<div class="padding-10">
    <input id="searchid" class="w-p98 select-icon" onkeyup="filter(this)" />
    <ul id="tree" class="ztree"></ul>
    <div class="text-center">
        <input type="button" value="确定" id="btnSave" class="btn" />
    </div>
</div>

